package com.bear.udisk.handler;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bear.udisk.model.Userinfo;
import com.bear.udisk.service.UserinfoService;

@Controller
@RequestMapping("/user")
public class UserinfoHandler {

	@Autowired
	private UserinfoService userinfoService;

	/**
	 * 用户注册
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/register")
	public String register(Userinfo user) {
		boolean isSuccess = userinfoService.register(user);
		if (isSuccess) {
			return "redirect:/login.jsp";// 成功之后跳转登陆页面
		}
		return "redirect:/register.jsp";// 失败后跳转注册页面
	}

	/**
	 * 用户登陆
	 * 
	 * @param userName
	 * @param userPass
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String userName, String userPass, HttpSession session) {
		Userinfo user = userinfoService.login(userName, userPass);
		if (user != null) {// 登陆成功
			session.setAttribute("user", user);
			return "redirect:/fileinfo/list.action";
		}
		return "login";

	}

}
